<template>
    <div class="components_edit_modal">
        <el-dialog width="1000px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="info" :model="info" :rules="rules" label-width="110px" size="mini">
                <el-form-item label="轮播图分类：" prop="typeId">
                    <el-select v-model="info.typeId" filterable placeholder="请选择轮播图分类">
                        <el-option v-for="item in options" :key="item.id" :label="item.dictValue" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="图片地址：" prop="url">
                    <el-input type="textarea" v-model.trim="info.url" placeholder="请输入轮播图描述" style="display:none;"></el-input>
                    <pl-upload :postData="attachment" :displaymode="'images'" :api="'jstn-pic'" :imageOnly="false" :buttonIds='{select:"selectfiles1",upload:"postfiles1",container:"container1"}' @reloadFiles="reloadFiles"></pl-upload>
                </el-form-item>
                <el-form-item label="链接地址：" prop="link">
                    <el-input v-model.trim="info.link" placeholder="请输入轮播图链接地址"></el-input>
                </el-form-item>
                <el-form-item label="轮播图标题：" prop="title">
                    <el-input v-model.trim="info.title" placeholder="请输入轮播图标题"></el-input>
                </el-form-item>
                <el-form-item label="排序：" prop="sort">
                    <el-input-number v-model="info.sort" :min="1" :max="10" label="请输入轮播图排序"></el-input-number>
                </el-form-item>
                <el-form-item label="轮播图描述：" prop="description">
                    <el-input type="textarea" v-model.trim="info.description" placeholder="请输入轮播图描述"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="mini" @click="addinfo">确 定</el-button>
                <el-button @click="state = false" size="mini">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {
        enums
    } from "@/constant";
    import PlUpload from "../../../common/PlUpload";
    export default {
        components: {
            "pl-upload": PlUpload
        },
        data() {
            return {
                state: false,
                title: "",
                type: "",
                imageUrl: "",
                attachment: [], //上传附件
                info: {
                    typeId: null,
                    url: null,
                    link: null,
                    title: null,
                    sort: 1,
                    description: null
                },
                rules: {
                    typeId: [{
                        required: true,
                        message: "请选择轮播图分类",
                        trigger: "blur"
                    }],
                    url: [{
                        required: true,
                        message: "请输入图片地址",
                        trigger: "blur"
                    }],
                    link: [{
                        required: true,
                        message: "请输入链接地址",
                        trigger: "blur"
                    }],
                    title: [{
                        required: true,
                        message: "请输入轮播图标题",
                        trigger: "blur"
                    }],
                    sort: [{
                        required: true,
                        message: "请输入轮播图排序",
                        trigger: "blur"
                    }],
                    description: [{
                        required: true,
                        message: "请输入轮播图描述",
                        trigger: "blur"
                    }]
                },
                options: []
            };
        },
        created() {},
        mounted() {
            this.getBannerType();
        },
        methods: {
            showDialog(val) {
                if (this.$refs["info"] !== undefined) {
                    this.$refs["info"].resetFields('');
                }
                this.info = {};
                if (val) {
                    for (var key in val) {
                        this.info[key] = val[key];
                    }
                    this.title = "编辑轮播图";
                    this.type = "edit";
                } else {
                    this.title = "添加轮播图";
                    this.type = "add";
                }
                this.state = true;
            },
            // 获取banner类型
            getBannerType() {
                this.axios.get(api.dictionary.dictList + "?type=banner")
                    .then(response => {
                        if (response.data.code == 200) {
                            this.options = response.data.data.dataList;
                        } else {
                            this.$message({
                                message: response.data.message,
                                type: "error",
                                center: true
                            });
                        }
                    });
            },
            addinfo() {
                let vm = this;
                vm.$refs.info.validate(valid => {
                    if (valid) {
                        let submission = ""; //提交方式
                        //选择提交方式
                        if (vm.type == "edit") {
                            submission = "put";
                        } else {
                            submission = "post";
                        }
                        // 提交
                        this.axios({
                            method: submission,
                            url: api.backendsManagement.backendList,
                            data: vm.info
                        }).then(response => {
                            if (response.data.code == 200) {
                                console.log(response.data)
                                vm.state = false;
                                vm.$parent.$refs.tableList.$emit("vuetable:refresh");
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            reloadFiles: function(files) {
                let vm = this;
                this.attachment = [];
                files.forEach((item, idnex) => {
                    item.url = item.fileStoreName;
                    this.attachment.push(item)
                })
                this.info.url = this.attachment[0].url;
            }
        }
    };
</script>
<style scoped>

</style>